package com.swallow.auth.infrastructure.mysql.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swallow.auth.infrastructure.mysql.mapper.AccountMapper;
import com.swallow.auth.infrastructure.mysql.po.AccountPO;
import com.swallow.auth.infrastructure.mysql.service.IAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 账号表 服务实现类
 * </p>
 *
 * @author 18310740596@163.com
 * @since 2024-05-11
 */
@Slf4j
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountPO> implements IAccountService, UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("当前登录用户:{}", username);

        AccountPO account = this.lambdaQuery()
                                .eq(AccountPO::getDeleteFlag, 0)
                                .and(w -> w.eq(AccountPO::getAccountName, username)
                                           .or()
                                           .eq(AccountPO::getMobile, username))
                                .one();

        if (Objects.isNull(account)) {
            throw new UsernameNotFoundException("用户信息不存在");
        }

        // 目前先设置默认值
        account.setTenantNo("212024051400000000");
        account.setTenantName("燕子芡商城");
        account.setStaffName("张三");

        return account;
    }
}
